<?php

/**
 * This is the model class for table "tbl_blog".
 *
 * The followings are the available columns in table 'tbl_blog':
 * @property integer $id
 * @property string $title
 * @property string $description
 * @property string $read_sum
 * @property string $tag
 * @property integer $type_id
 * @property string $create_date
 * @property string $update_date
 */
class Blog extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Blog the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_blog';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
            array('title', 'required','message'=>'文章标题不能为空'),
            array('description', 'required', 'message'=>'文章内容不能为空'),
            array('user_id', 'required', 'message'=>'用户不能为空'),
			array('type_id', 'numerical', 'integerOnly'=>true),
			array('title', 'length', 'max'=>255),
			array('read_sum', 'length', 'max'=>20),
			array('tag, source_url, update_date', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, title, source_url, description, read_sum, tag, type_id, user_id, create_date, update_date', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations(){
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
            'type'=>array(self::BELONGS_TO, 'Type', 'type_id'),
            'user'=>array(self::BELONGS_TO, 'User', 'user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'title' => '文章标题',
			'description' => '文章内容',
			'read_sum' => '阅读数量',
			'tag' => '文章标签',
            'source_url' => '文章转载地址',
			'type_id' => '文章类型',
            'user_id' => '用户的角色',
			'create_date' => '创建时间',
			'update_date' => '更新时间',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('read_sum',$this->read_sum,true);
		$criteria->compare('tag',$this->tag,true);
		$criteria->compare('type_id',$this->type_id);
        $criteria->compare('user_id',$this->user_id);
		$criteria->compare('create_date',$this->create_date,true);
		$criteria->compare('update_date',$this->update_date,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
    
    /**
	 * 行为操作
	 */
	public function behaviors(){
		return array(
				'CTimestampBehavior' => array(
						'class' => 'zii.behaviors.CTimestampBehavior',
						'createAttribute' => 'create_date',
						'updateAttribute' => 'update_date',
						'setUpdateOnCreate' => 'true',
				)

		);
	}
    
    public function beforeSave(){
        if(parent::beforeSave()){
//            $this->setAttribute('user_id',Yii::app()->admin->id);
        }
        return true;
    }
}